package uns.fit.gis.ui;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.game.Sprite;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.audio.Sound;
import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.MapCanvas;

public class BestRouteList extends List implements CommandListener {
	private Sprite turningSprite;
	private Command backCommand;
	private Command readCommand;
	private MapDrawer midlet;
	private static Font font;
	
	public BestRouteList(MapDrawer midlet) {
		super("", List.IMPLICIT);
		this.midlet = midlet;
		backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 1);
		addCommand(backCommand);
		readCommand = new Command(LocaleManager.getMessage("read"), Command.ITEM, 1);
		addCommand(readCommand);
		setCommandListener(this);
		font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		
		try {
			turningSprite = new Sprite(Image.createImage("/turnicon.png"), 16, 16);
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public void commandAction(Command command, Displayable displayable) {
		if(command == backCommand) {
			midlet.showMapCanvas();
		}
		else if(command == readCommand) {
			if(MapCanvas.alg != null) {
				String route = MapCanvas.alg.routeToSoundString();
				String[] streets = Util.split(route, "\n");
				Sound.readString(streets);
			}
		}
	}
	
	public void createRoute() {
		this.deleteAll();
		if(MapCanvas.alg != null) {
			String route = MapCanvas.alg.routeToString();
			Image img = Image.createImage(16, 16);
			Graphics g = img.getGraphics();
			if(route != null) {
				this.setTicker(new Ticker(LocaleManager.getMessage("totallength") + ": " + MapCanvas.alg.getTotalLength() + "m"));
				String[] subroutes = Util.split(route, "\n");
				
				for(int i = 0; i < subroutes.length; i ++){
					if(subroutes[i].equalsIgnoreCase(Util.GO_STRAIGHT)) {
						turningSprite.setFrame(1);
						turningSprite.setPosition(0, 0);
						turningSprite.paint(g);
						this.append("", img);
					}
					else if(subroutes[i].equalsIgnoreCase(Util.TURN_LEFT)) {
						turningSprite.setFrame(0);
						turningSprite.setPosition(0, 0);
						turningSprite.paint(g);
						this.append("", img);
					}
					else if(subroutes[i].equalsIgnoreCase(Util.TURN_RIGHT)) {
						turningSprite.setFrame(2);
						turningSprite.setPosition(0, 0);
						turningSprite.paint(g);
						this.append("", img);
					}
					else {
						this.append(subroutes[i], null);
					}
                                        //setFont(i, font);
				}
			}
		}
	}
}
